var number = 0;
var Observe = function(vm,data){
    if(!data || typeof data !== 'object'){
        return false
    }
    this.vm = vm;
    var dep = new Dep();
    this.data = data;
    Object.keys(data).map(key=>{
        // console.log(key)
        number+=1;
        this.directive(key,data[key],dep)
    })
}

Observe.prototype = {
    directive(key,value,dep){
        Observe(value);
        Object.defineProperty(this.data,key,{
            enumerable:true,
            configurable:true,
            set(new_val){
                // console.log(new_val)
                if(value === new_val){
                    return ;
                }
                value = new_val;

                dep.notify();
                return value;
            },
            get(){
                if(Dep.target){
                    dep.linkWatcher();
                }
                return value;
            }
        })
    }
}

var Dep = function(){
    this.subs = [];
    this.notifyies = [];
    this.obj ={};
}

Dep.prototype = {
    notify(){
        // console.log(this.subs)
        // var subs = this.removeSub();
        // console.log(subs)
        this.subs.map(sub=>{
            sub.update();
        })
    },
    addWatcher(watcher){
        // if(this.subs.length<number){
            this.subs.push(watcher)
        // }
        // console.log('subs',this.subs)
        // console.log(432141,this.subs.length)
    },
    removeSub(){
        this.subs.map((sub,i)=>{
            // console.log(i)
            // console.log(this.subs)
            if(this.obj [sub.$exp]){
                this.obj [sub.$exp] = 1;
            }else{
                this.notifyies.push(sub)
                this.obj [sub.$exp] = 1;
            }
        })

        return this.notifyies;
    },
    linkWatcher(){
        // console.log( Dep.target)
        Dep.target.addDep(this);
    }
}

Dep.target = null;